//
//  XMLComposeToolBar.swift
//  Weibo
//
//  Created by mac on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
enum XMLComposeToolBarButtonType :Int {
    // 图片
    case Picture = 0
    // @
    case Mention = 1
    // 话题
    case Trend = 2
    // 表情
    case Emoticon = 3
    // Add
    case Add = 4
}

class XMLComposeToolBar: UIStackView {
    
    var didSelectedButtonClosure :((buttonType :XMLComposeToolBarButtonType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        axis = .Horizontal
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", buttonType: .Picture)
        addChildButton("compose_mentionbutton_background", buttonType: .Mention)
        addChildButton("compose_trendbutton_background", buttonType: .Trend)
        addChildButton("compose_emoticonbutton_background", buttonType: .Emoticon)
        addChildButton("compose_add_background", buttonType: .Add)
    }
    
    private func addChildButton(imageName :String,buttonType :XMLComposeToolBarButtonType) {
    
        let button = UIButton()
        button.tag = buttonType.hashValue
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        button.adjustsImageWhenHighlighted = false
        button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
        addArrangedSubview(button)
    }
    
    @objc private func buttonClick(button :UIButton) {
    
        let type = XMLComposeToolBarButtonType(rawValue: button.tag)!
        
        didSelectedButtonClosure?(buttonType :type)
        
    }

}
